package com.iemes.controller.hr;

/**
 * 培训计划
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.TrainFormMap;
import com.iemes.entity.TrainPeopleFormMap;
import com.iemes.mapper.hr.TrainMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("/hrtrain/")
public class TrainController extends BaseController {

	@Inject
	private TrainMapper trainMapper;

	// 培训
	@RequestMapping("trainAttend")
	public String examineOrder(Model model) {
		System.out.println("================>进入");
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/hr/train_list";
	}

	// 培训分页
	@ResponseBody
	@RequestMapping("train_findByPage")
	public PageView findByPage(String pageNow, String pageSize)
			throws Exception {

		TrainPeopleFormMap trainPeopleFormMap = getFormMap(TrainPeopleFormMap.class);
		List<Map<String, Object>> trainPeopleFormMaps = trainMapper.getAllTrain(trainPeopleFormMap);
		LuogicPaging(pageNow, pageSize, trainPeopleFormMaps);
		pageView.setRecords(trainPeopleFormMaps);
		return pageView;
	}

	// 培训计划详情页面
	@RequestMapping("train_particulars")
	public String examineOrders(Model model) {
		//拿培训计划的表的id
		String id = getPara("id");
		TrainPeopleFormMap trainPeopleFormMap =getFormMap(TrainPeopleFormMap.class);
		TrainFormMap trainFormMap=trainMapper.findbyFrist("id", id, TrainFormMap.class);
		trainPeopleFormMap.put("where", "where records_id="+id);
	    //通过查询培训人员表 得到员工id 可以得知有多少员工参加了 	
		List<TrainPeopleFormMap> listTrainPeopleFormMap=trainMapper.findByWhere(trainPeopleFormMap);
		List<String> peopleId=new ArrayList<String>();
		for(TrainPeopleFormMap form:listTrainPeopleFormMap){
			peopleId.add(form.get("id").toString());
		}
		List<Map<String,Object>> listStaffFormMap = trainMapper.getAllPeople(peopleId);

		Integer count=0;//缺勤
		Integer counts=0;//参与
		Integer calculate=0;//应到人数
		Integer leave=0;//请假人数
		Integer latecomer=0;//迟到人数
		for (Map<String, Object> map : listStaffFormMap) {
			String state= map.get("state").toString();
			Integer number=Integer.valueOf(state);
			if(number==3){
				leave+=1;
			}
			if(number==4){
				latecomer+=1;
			}
			if(number==2||number==3){
				count+=1;
			}
			if(number!=2&&number!=3){
				counts+=1;
			}
			calculate+=1;
		}
		model.addAttribute("count",count);
		model.addAttribute("counts",counts);
		model.addAttribute("calculate",calculate);
		model.addAttribute("leave",leave);
		model.addAttribute("latecomer",latecomer);
		model.addAttribute("trainPeople",listTrainPeopleFormMap);
		model.addAttribute("staff",listStaffFormMap);
		model.addAttribute("train",trainFormMap);
		return Common.BACKGROUND_PATH + "/hr/train_particulars";
	}

	//修改详情
		@ResponseBody
		@RequestMapping(value="train_particulars_operation")
		@SystemLog(module="培训计划数据维护",methods="查看培训计划详情-修改详情")//凡需要处理业务逻辑的.都需要记录操作日志
		public String editEntity() {
			//获取jsp动态生成的文本框的值
			HttpServletRequest request=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
			String[] TrainPeopleForId=request.getParameterValues("trainPeopleFormMap.id");
			String[] oneState=request.getParameterValues("oneState");
			String[] TrainPeopleForIds=request.getParameterValues("trainPeopleFormMap.ids");
			String[] twoState=request.getParameterValues("twoState");
			TrainPeopleFormMap trainPeopleFormMap = getFormMap(TrainPeopleFormMap.class);
			TrainPeopleFormMap trainPeopleFormMaps = getFormMap(TrainPeopleFormMap.class);
			TrainFormMap trainFormMap=getFormMap(TrainFormMap.class);
			try {
				trainMapper.editEntity(trainFormMap);
				
			for (int i = 0; i < TrainPeopleForId.length; i++) {
				trainPeopleFormMap.put("id", TrainPeopleForId[i]);
				trainPeopleFormMap.put("state", oneState[i]);
				trainMapper.editEntity(trainPeopleFormMap);
				trainPeopleFormMaps.put("id", TrainPeopleForIds[i]);
				trainPeopleFormMaps.put("state", twoState[i]);
				trainMapper.editEntity(trainPeopleFormMaps);
			}
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
			return "success";
		}
	
}
